[NSData dataWithContentsOfFile:path] doesn't work

Posted by Felics on Stack Overflow See other posts from Stack Overflow or by Felics
Published on 2010-05-31T16:47:27Z Indexed on 2010/05/31 16:53 UTC
Read the original article Hit count: 206

Filed under:
|

Hello, when I have the fallowing code to read a binary file:

NSString* file = [NSString stringWithUTF8String:fileName];

NSString* filePath = resource ? [[NSBundle mainBundle] pathForResource:file ofType:nil] : [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent: file];

NSData* fileData = [NSData dataWithContentsOfFile:filePath];

Where "fileName" and resource are load function parameters. "resource" is used to know if the file is located in application bundle or in Documents. Sometimes this code works well and sometimes it doesn't. As far I saw this problem is random. I can run the code 10 times in a row and it works fine and after that it gives me nil data without any modification. Does anybody knows what could be the problem? Could it be related with file extension or file name?

Thank you.

PS: I use this code on iPhone Simulator and the file exists in application bundle.

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about file-io